package com.teach.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.teach.R;
import com.teach.dto.LoginUser;
import com.teach.dto.ShopsSearch;
import com.teach.entity.Shops;
import com.teach.service.ShopsService;

/**<pre>
 * TODO:店铺
 * ClassName : com.teach.controller.ShopsController
 * Author : J.L.Zhou
 * Date : 2019年8月14日 下午7:11:46
 * Version : V1.0
 * Copyright 2019 jlzhou.top Inc. All rights reserved. 
 * Warning：本内容仅限于公司内部传阅，禁止外泄以及用于其他的商业目.</pre>
 */
@RestController
@RequestMapping("/shops")
public class ShopsController {

	@Autowired
	private ShopsService service;
	@PostMapping("add.do")
	public R doAdd(Shops shops,Double lng,Double lat,MultipartFile[] imgs,MultipartFile[] files,LoginUser user) {
		shops.setUserName(user.getUserName());
		service.add(shops, lng, lat, imgs,files);
		return R.ok();
	}
	
	@PostMapping("modify.do")
	public R doModify(Shops shops,Double lng,Double lat,int[] deleteImgs,MultipartFile[] imgs,MultipartFile[] files) {
		service.modify(shops, lng, lat, deleteImgs, imgs,files);
		return R.ok();
	}
	
	@PostMapping("pass.do")
	public R doPass(Shops shops,LoginUser user) {
		shops.setShopPassUserName(user.getUserName());
		service.modifyPass(shops);
		return R.ok();
	}
	
	@DeleteMapping("delete.do")
	public R doDelete(Long id) {
		service.delete(id);
		return R.ok();
	}
	
	@GetMapping("get.do")
	public R doGet(Long id,LoginUser user) {
		if(user!=null&&user.getRoleId()==999) {
			return R.ok().put("shops", service.get(id)).put("pass", true);
		}else {
			Shops s = service.get(id);
			s.setCertificates(null);
			return R.ok().put("shops", s).put("pass", false);
		}
		
	}
	
	@GetMapping("near.do")
	public R doNear(ShopsSearch search) {
		return R.ok().put("list", service.list(search));
	}
	
	@GetMapping("page.do")
	public Object doPage(ShopsSearch search) {
		return service.page(search);
	}
	
	
	@GetMapping("apply.do")
	public R doApply(LoginUser user) {
		return R.ok().put("roleId", user.getRoleId()).put("shops", service.getByUserName(user.getUserName()));
	}
}
